File: Language\Legacy\DisposableAction`1.cs
Web Access
Project: src\src\Razor\src\Compiler\Microsoft.CodeAnalysis.Razor.Compiler\src\Microsoft.CodeAnalysis.Razor.Compiler.csproj (Microsoft.CodeAnalysis.Razor.Compiler)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System;
 
namespace Microsoft.AspNetCore.Razor.Language.Legacy;
 
internal ref struct DisposableAction<T>
{
    private readonly Action<T> _action;
    private readonly T _arg;
    private bool _invoked;
 
    public DisposableAction(Action<T> action, T arg)
    {
        _action = action ?? throw new ArgumentNullException(nameof(action));
        _arg = arg;
    }
 
    public void Dispose()
    {
        if (!_invoked)
        {
            _action(_arg);
            _invoked = true;
        }
    }
}